package classes.Windows;

import db.Jdbc;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: 程嘉明
 * @Date: 2018/8/26/026 08:48
 * @Description: 工人的窗口
 * 工人登录的主界面
 * 操作有：在规定的时间内打卡、查看自己的上班打卡情况、查看自己的工资、以及工资的发放
 */
public class WinWorker extends Window {
    //    private Worker self=new Worker();
    public WinWorker(int length, int width, String title, String ID, String NAME) throws Exception {
        super(length, width, title);
        //标题
        JLabel lab3 = new JLabel("工人操作界面");
        lab3.setBounds(0, 20, 800, 60);
        lab3.setFont(new Font("宋体", 1, 18));
        lab3.setOpaque(true);
        lab3.setBackground(Color.blue);
        this.addP(lab3);
        //用户标签
        JLabel lab1 = new JLabel("用户姓名：");
        lab1.setBounds(0, 70, 120, 60);
        lab1.setFont(new Font("宋体", 1, 14));
        this.addP(lab1);
        JLabel lab2 = new JLabel(NAME);
        lab2.setBounds(70, 70, 100, 60);
        lab2.setFont(new Font("宋体", 1, 14));
        this.addP(lab2);
        //ID标签
        JLabel lab4 = new JLabel("ID:");
        lab4.setBounds(140, 70, 120, 60);
        lab4.setFont(new Font("宋体", 1, 16));
        this.addP(lab4);
        JLabel lab5 = new JLabel(ID);
        lab5.setFont(new Font("宋体", 1, 16));
        lab5.setBounds(170, 70, 120, 60);
        this.addP(lab5);
        //帮助按钮
        JButton jb1 = new JButton("帮助");
        jb1.setBounds(400, 85, 180, 40);
        jb1.setBackground(Color.yellow);
        jb1.addActionListener(e -> {
            JOptionPane.showMessageDialog(null, "您现在处于工人操作界面您可以" +
                    "查看你的上班情况和工资发放情况。\n                  如有问题请联系客服QQ：2239539295。", "帮助", JOptionPane.INFORMATION_MESSAGE);
        });
        this.addP(jb1);
        //退出按钮
        JButton jb2 = new JButton("退出");
        jb2.addActionListener(e -> {
            System.exit(0);
        });
        jb2.setBounds(600, 85, 180, 40);
        jb2.setBackground(Color.red);
        this.addP(jb2);
        //获取当前时间
        JLabel lab6 = new JLabel("当前北京时间：");
        lab6.setBounds(0, 140, 120, 60);
        lab6.setFont(new Font("宋体", 1, 14));
        this.addP(lab6);
        JTextField time = new JTextField();
        time.setEnabled(false);//设置不可修改
        time.setBounds(100, 150, 180, 40);
        SimpleDateFormat myfmt = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        class TimeActionListener implements ActionListener {
            public TimeActionListener() {
                Timer t = new Timer(1000, this);
                t.start();
            }

            @Override
            public void actionPerformed(ActionEvent ae) {
                time.setText(myfmt.format(new Date()).toString());
            }
        }
        time.addActionListener(new TimeActionListener());
        this.addP(time);
        //上班打卡
        AtomicInteger num = new AtomicInteger(0);//记录打卡次数，当打卡次数为2时在数据库中插入数据,线程安全
        JButton jb3 = new JButton("上班打卡");
        jb3.setBounds(400, 150, 180, 40);
        jb3.addActionListener(e -> {
            try {
                Date now = new Date();
                if (IsTimeM(now)) {
                    //早上打卡时间正确
                    Jdbc.getConnection();
                    SimpleDateFormat myfmt1 = new SimpleDateFormat("hh:mm:ss");//确定时间格式
                    now = myfmt1.parse(now.toString().split(" ")[3]);//获得当前时间的格式
                    now = new java.sql.Date(new Date().getTime());//转化可以插入mysql
                    ResultSet rs = Jdbc.stat.executeQuery("select count(M) from clockin where time='" + now + "' and ID='" + ID + "';");//查询是否在早上打卡
                    rs.next();
                    int exist = rs.getInt("count(M)");
                    if (exist == 0) {//没数据说明没打卡，插入记录
                        String inster = "insert into clockin values('" + ID + "','" + now + "',1,0,'" + NAME + "');";
                        Jdbc.stat.executeUpdate(inster);
                        JOptionPane.showMessageDialog(null, "上午打卡成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                    } else {//说明打卡了在早上
                        JOptionPane.showMessageDialog(null, "你已经在上午打卡了！", "警告", JOptionPane.INFORMATION_MESSAGE);
                    }
                }

            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
        this.addP(jb3);
        JButton jb4 = new JButton("下班打卡");
        jb4.setBounds(600, 150, 180, 40);
        jb4.addActionListener(e -> {
            try {
                Date now = new Date();
                if (IsTimeE(new Date())) {
                    //晚上打卡时间正确
                    Jdbc.getConnection();
                    SimpleDateFormat myfmt1 = new SimpleDateFormat("hh:mm:ss");//确定时间格式
                    now = myfmt1.parse(now.toString().split(" ")[3]);//获得当前时间的格式
                    now = new java.sql.Date(new Date().getTime());//转化可以插入mysql
                    ResultSet rs1 = Jdbc.stat.executeQuery("select M from clockin where time='" + now + "' and ID='" + ID + "';");//查询是否存在
                    rs1.next();
                    ResultSet rs2 = Jdbc.stat1.executeQuery("select E from clockin where time='" + now + "' and ID='" + ID + "';");//查询是否存在
                    rs2.next();
                    int E = rs2.getInt("E");
                    int M = rs1.getInt("M");
                    try {
                        if (M == 1 && E == 0) {//上午打卡了，下午没打卡
                            Jdbc.stat.executeUpdate("update clockin set E=1;");
                            JOptionPane.showMessageDialog(null, "下午打卡成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                            //在工资表中去修改记录
                            ResultSet rs3 = Jdbc.stat1.executeQuery("select Daysh from salary where ID='" + ID + "'");
                            rs3.next();
                            int Daysh = rs3.getInt("Daysh") + 1;
                            System.out.println(rs3);
                            Jdbc.stat.executeUpdate("update salary set Daysh='" + Daysh + "' where ID='" + ID + "'");
                        } else {
                            if (M == 1 && E == 1) {
                                JOptionPane.showMessageDialog(null, "下午已经打卡了！", "提示", JOptionPane.INFORMATION_MESSAGE);
                            } else {
                                if (M == 0) {
                                    JOptionPane.showMessageDialog(null, "你在上午没有打卡！", "警告", JOptionPane.WARNING_MESSAGE);
                                }
                            }
                        }
                    } catch (SQLException sqle) {//跑出异常说明上午没有打卡，不存在记录
                        JOptionPane.showMessageDialog(null, "你在上午没有打卡！", "警告", JOptionPane.WARNING_MESSAGE);
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
        this.addP(jb4);
        JLabel lab7 = new JLabel(NAME + "的工资情况:");
        lab7.setFont(new Font("宋体", 1, 16));
        lab7.setBounds(0, 200, 180, 80);
        this.addP(lab7);
        JLabel lab8 = new JLabel("        待查询");
        lab8.setFont(new Font("宋体", 1, 16));
        lab8.setBounds(150, 200, 400, 80);
        this.addP(lab8);
        JButton jb5 = new JButton("查询");
        jb5.setBounds(600, 220, 180, 40);
        this.addP(jb5);
        jb5.addActionListener(e -> {
            try {
                Jdbc.getConnection();
                ResultSet rs = Jdbc.stat2.executeQuery("select * from salary where ID='" + ID + "';");//查找工资
                rs.next();
                lab8.setFont(new Font("宋体", 1, 14));
                lab8.setText("基本工资:" + rs.getInt("base") + "提成:" + rs.getInt("commission") + "实际发放工资为;" + rs.getInt("net payroll"));
                jb5.setText("刷新");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });
        JLabel lab9 = new JLabel("上班打卡情况:");
        lab9.setBounds(0, 260, 180, 40);
        lab9.setFont(new Font("宋体", 1, 16));
        this.addP(lab9);
        JLabel lab10 = new JLabel("        待查询");
        lab10.setBounds(150, 260, 180, 40);
        lab10.setFont(new Font("宋体", 1, 16));
        this.addP(lab10);
        JButton jb6 = new JButton("查询");
        jb6.setBounds(600, 260, 180, 40);
        this.addP(jb6);
        jb6.addActionListener(e -> {
            try {
                Jdbc.getConnection();
                ResultSet rs = Jdbc.stat.executeQuery("select Daysh from salary  where ID='" + ID + "'");
                rs.next();
                lab10.setText("实到" + new Integer(rs.getInt("Daysh")).toString() + "天");
                lab10.setFont(new Font("宋体", 1, 16));
                jb6.setText("刷新");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });

        //查询工地表格
        JLabel lab11 = new JLabel("工地表格");
        lab11.setBounds(0, 320, 120, 40);
        lab11.setFont(new Font("宋体", 1, 16));
        this.addP(lab11);
        String[] title1 = {"工地ID", "工地标识", "工地名称", "工地地址", "合同编号", "施工许可证", "所属区域"};
        Object[][] rows = new Object[][]{{"待查询", "待查询", "待查询", "待查询", "待查询", "待查询", "待查询"}};
        DefaultTableModel  model = new DefaultTableModel(rows, title1);
        JTable tab = new JTable(model);
        JScrollPane jp = new JScrollPane(tab);//滚动版面
        tab.setBounds(0, 0, 500, 200);
        jp.setBounds(0, 350, 500, 200);
        tab.setPreferredScrollableViewportSize(new Dimension(550, 100));
        this.addP(jp);//最后加入滚动版面
        JButton jb8 = new JButton("查询");
        jb8.setBounds(600, 320, 180, 40);
        AtomicBoolean click= new AtomicBoolean(false);//是否点击
        jb8.addActionListener(e -> {
            if(!click.get()){
                model.removeRow(0);
                try {
                    Jdbc.getConnection();
                    String sql = "select * from t_gdsbxx;";//查询所有的工地
                    ResultSet rs = Jdbc.stat.executeQuery(sql);
                    int i = 0;
                    int j = 0;
                    while (rs.next()) {
                        ArrayList<String> row=new ArrayList<>();
                        row.add(rs.getString("ID"));
                        row.add(rs.getString("GDBS"));
                        row.add(rs.getString("GDMC"));
                        row.add(rs.getString("GDDZ"));
                        row.add(rs.getString("HTBH"));
                        row.add(rs.getString("SGXKZBH"));
                        row.add(rs.getString("PACE"));
                        model.insertRow(0,row.toArray());
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                click.set(true);
                jb8.setText("刷新");
            }else{
                JOptionPane.showMessageDialog(null, "刷新成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
            }

        });


        this.addP(jb8);
    }//构造函数

    public static void main(String[] args) throws Exception {
        new WinWorker(800, 600, "工人操作界面", "T000001", "李武");
    }

    /**
     * 早上打卡时间函数
     */
    public boolean IsTimeM(Date now) throws Exception {
        SimpleDateFormat myfmt1 = new SimpleDateFormat("hh:mm:ss");//确定时间格式
        now = myfmt1.parse(now.toString().split(" ")[3]);//获得当前时间的格式
        Date begin = myfmt1.parse("08:00:00");//早上打卡开始时间
        Date end = myfmt1.parse("09:00:00");//早上打卡开结束时间
        if (now.getTime() < end.getTime() && now.getTime() > begin.getTime()) {//在规定时间段内打卡
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "请在规定时间内打卡！", "警告", JOptionPane.WARNING_MESSAGE);
            return false;
        }

    }

    /**
     * 晚上打卡时间函数
     */
    public boolean IsTimeE(Date now) throws Exception {
        SimpleDateFormat myfmt1 = new SimpleDateFormat("hh:mm:ss");//确定时间格式
        now = myfmt1.parse(now.toString().split(" ")[3]);//获得当前时间的格式
        Date begin = myfmt1.parse("20:00:00");//早上打卡开始时间
        Date end = myfmt1.parse("21:00:00");//早上打卡开结束时间
        if (now.getTime() < end.getTime() && now.getTime() > begin.getTime()) {//在规定时间段内打卡
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "请在规定时间内打卡！", "警告", JOptionPane.WARNING_MESSAGE);
            return false;
        }
    }
}
